package com.cash.loan.modular.cash.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cash.loan.core.upload.QiniuService;
import com.cash.loan.modular.cash.dao.CashVersionMapper;
import com.cash.loan.modular.cash.model.CashVersion;
import com.cash.loan.modular.cash.result.PageResult;
import com.cash.loan.modular.cash.service.CashVersionService;
import com.cash.loan.modular.cash.transfer.CashVersionDto;
import com.cash.loan.modular.cash.transfer.UpdateCashVersionDto;
import com.cash.loan.modular.cash.util.PageBeanUtil;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Administrator
 * @Date 2018-10-08 11:36
 */
@Service
public class CashVersionServiceImpl implements CashVersionService {

    @Autowired
    private CashVersionMapper cashVersionMapper;

    @Autowired
    private QiniuService qiniuService;

    @Override
    public PageResult<CashVersion> list(Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        List<CashVersion> cashVersionList = cashVersionMapper.selectList(new EntityWrapper<CashVersion>());
        cashVersionList.stream().forEach(cashVersion ->
            cashVersion.setFilePath(qiniuService.getUrl(cashVersion.getFilePath())));
        PageResult result = PageBeanUtil.toPageResult(cashVersionList);
        return result;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean add(CashVersionDto cashVersion) {
        CashVersion newCashVersion = new CashVersion();
        BeanUtils.copyProperties(cashVersion,newCashVersion);
        newCashVersion.setFilePath(qiniuService.getUri(newCashVersion.getFilePath()));
        cashVersionMapper.insert(newCashVersion);
        return true;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean update(UpdateCashVersionDto cashVersion) {
        CashVersion newCashVersion = new CashVersion();
        BeanUtils.copyProperties(cashVersion,newCashVersion);
        newCashVersion.setFilePath(qiniuService.getUri(newCashVersion.getFilePath()));
        cashVersionMapper.updateById(newCashVersion);
        return true;
    }

    @Override
    public CashVersion detail(Integer id) {
        CashVersion cashVersion = cashVersionMapper.selectById(id);
        cashVersion.setFilePath(qiniuService.getUrl(cashVersion.getFilePath()));
        return cashVersion;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean updateStatus(Integer id, Integer status) {
        cashVersionMapper.updateStatus(id,status);
        return true;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean delete(Integer id) {
        cashVersionMapper.deleteById(id);
        return true;
    }
}
